/**********************************************************************
* This file is part of Adempiere ERP Bazaar                           *
* http://www.adempiere.org                                            *
*                                                                     *
* Copyright (C) Duman Zhunissov                                       *
*                                                                     *
* This program is free software; you can redistribute it and/or       *
* modify it under the terms of the GNU General Public License         *
* as published by the Free Software Foundation; either version 2      *
* of the License, or (at your option) any later version.              *
*                                                                     *
* This program is distributed in the hope that it will be useful,     *
* but WITHOUT ANY WARRANTY; without even the implied warranty of      *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the        *
* GNU General Public License for more details.                        *
*                                                                     *
* You should have received a copy of the GNU General Public License   *
* along with this program; if not, write to the Free Software         *
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,          *
* MA 02110-1301, USA.                                                 *
*                                                                     *
* Contributors:                                                       *
* - Duman Zhunissov                                                   *
*                                                                     *
* Sponsor:                                                           *
* - "ERP-Service "KazTransCom" LLP                                    *
**********************************************************************/
package org.compiere.FA;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import org.compiere.model.MAsset;
import org.compiere.model.X_A_Asset;
import org.compiere.process.ProcessInfoParameter;
import org.compiere.process.SvrProcess;
import org.compiere.util.CLogger;
import org.compiere.util.DB;
import org.compiere.util.Msg;

/**
 *	Post Depreciation
 *	
 *  @author Duman Znunissov
 *  @version $Id: DepreciationPost.java,v 1.0 $
 */

public class InternalMovementAdd extends SvrProcess
{
	/** Instance Logger 			*/
	private CLogger Log = CLogger.getCLogger(InternalMovementAdd.class);
	/** Parameters				*/
	private int p_AD_Org_ID = 0;
	private int	p_A_Asset_ID = 0;
	
	/**
	 *  Prepare - e.g., get Parameters.
	 */
	protected void prepare()
	{
		ProcessInfoParameter[] para = getParameter();
		for (int i = 0; i < para.length; i++)
		{
			String name = para[i].getParameterName();
			if (para[i].getParameter() != null) {
				if ("AD_Org_ID".equals(name))
					p_AD_Org_ID=Integer.valueOf(para[i].getParameter().toString());
				else if ("A_Asset_ID".equals(name))
					p_A_Asset_ID=Integer.valueOf(para[i].getParameter().toString());
				else
					log.info("prepare - Unknown Parameter: " + name);
			}
		}
	}	//	prepare

	
	/**
	 * 	Post Depreciation
	 *	@return info
	 *	@throws Exception
	 */
	protected String doIt() throws java.lang.Exception
	{
		String msg;
		log.info("doIt - Add Internal Movement =" + p_A_Asset_ID);

		int iAssetCount=0;
		StringBuffer sql =  new StringBuffer();
        
        sql.setLength(0);
        sql.append("SELECT a.A_Asset_ID, (SELECT COUNT(*) FROM A_InternalMovement"
        	+ "\n      WHERE A_Asset_ID = a.A_Asset_ID) as count"	
        	+ "\n FROM A_Asset AS a"	
    		+ "\n WHERE a.AD_Org_ID =" + p_AD_Org_ID + " AND a.AD_Client_ID="+getAD_Client_ID()
    	    + "\n AND a.isActive = 'Y'");
    	if (p_A_Asset_ID>0) sql.append(" AND a.A_Asset_ID = "+p_A_Asset_ID);
    	else sql.append("\n ORDER BY A_Asset_ID");
		PreparedStatement pstmt = null;
		pstmt = DB.prepareStatement (sql.toString(),null);
		ResultSet rs = null;
		try {				
		  rs = pstmt.executeQuery();
		  while (rs.next()){
			int iAssetID = rs.getInt("A_Asset_ID");
            if (iAssetID < 1) continue;
            int iInternal = rs.getInt(2);
            if (iInternal > 0) continue;
            ///X_A_Asset asset = new X_A_Asset (getCtx(), iAssetID, get_TrxName()); ///
            MAsset asset = new MAsset(getCtx(), iAssetID, get_TrxName());
			int iBPSRID= asset.getC_BPartnerSR_ID();
			if (iBPSRID < 1) continue;
			if (asset.TransferTo("1000000", asset.getAssetServiceDate(), iBPSRID, "", asset.getA_Location_ID())>0)
				iAssetCount++;
		  }
		} catch (Exception e) {
			  System.out.println("A_Depreciation_Exp. "+e);
		} finally {
			DB.close(rs, pstmt);
			rs = null; pstmt = null;
		}

		msg = Msg.translate(getCtx(), "Выполнено добавление по ") + iAssetCount
				+ " основным средствам.";
		Log.fine(msg);
		return msg;
	}	//	doIt

	
}
